
/**************************************************START OF FILE*****************************************************/





/*------------------------------------------------------------------------------------------------------------------
Includes
*/
/*FreeRTOS*/
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"

#include "xBSP_Gpio.h"
#include "xBSP_Spi.h"
#include "xSYS_ST7789.h"



/*------------------------------------------------------------------------------------------------------------------
Macros
*/
#define LCD_W   (240u)
#define LCD_H   (240u)


//LCD缓存大小设置
#define LCD_TOTAL_BUF_SIZE	(LCD_W*LCD_H*2)
#define LCD_BUF_SIZE 11520
static uint8_t lcd_buf[LCD_BUF_SIZE];

/*------------------------------------------------------------------------------------------------------------------
Variables
*/
static void LCD_SPI_Send(uint8_t *data, uint32_t size)
{
    uint32_t i,delta;
  
    delta = size/0xFFFF;
    
    for(i = 0; i<=delta; i++)
    {
        if( i==delta )  /* 发送最后一帧数据 */
            SPI3_WriteByte(&data[i*0xFFFF], size%0xFFFF);
        
        else    /* 超长数据一次发送0xFFFF字节数据 */
            SPI3_WriteByte(&data[i*0xFFFF], 0xFFFF);
    }
}


static void LCD_Write_Cmd(uint8_t cmd)
{
    LCD_DC(0);

    SPI3_WriteByte(&cmd, 1);
}

static void LCD_Write_Data(uint8_t data)
{
    LCD_DC(1);

    SPI3_WriteByte(&data, 1);
}


void LCD_Address_Set(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
    LCD_Write_Cmd(0x2a);
    LCD_Write_Data(x1 >> 8);
    LCD_Write_Data(x1);
    LCD_Write_Data(x2 >> 8);
    LCD_Write_Data(x2);

    LCD_Write_Cmd(0x2b);
    LCD_Write_Data(y1 >> 8);
    LCD_Write_Data(y1);
    LCD_Write_Data(y2 >> 8);
    LCD_Write_Data(y2);

    LCD_Write_Cmd(0x2C);
}

//void LCD_DisplayOn(void)
//{
//    LCD_PWR(1);
//}
//
//void LCD_DisplayOff(void)
//{
//    LCD_PWR(0);
//}

/*
********************************************************************************************************************
@ Brief  : spi io init

@ Param  : NONE

@ Return : NONE

@ Author : lyc

@  Date  : 2020 - 04 - 02
********************************************************************************************************************
*/
void xSYS_ST7789_Init(void)
{
    LCD_CS(0);
    LCD_PWR(0);

    LCD_RST(0);
    vTaskDelay(120);
    LCD_RST(1);

    vTaskDelay(120);
    /* Sleep Out */
    LCD_Write_Cmd(0x11);
    /* wait for power stability */
    vTaskDelay(120);

    /* Memory Data Access Control */
    LCD_Write_Cmd(0x36);
    LCD_Write_Data(0x00);

    /* RGB 5-6-5-bit  */
    LCD_Write_Cmd(0x3A);
    LCD_Write_Data(0x65);

    /* Porch Setting */
    LCD_Write_Cmd(0xB2);
    LCD_Write_Data(0x0C);
    LCD_Write_Data(0x0C);
    LCD_Write_Data(0x00);
    LCD_Write_Data(0x33);
    LCD_Write_Data(0x33);

    /*  Gate Control */
    LCD_Write_Cmd(0xB7);
    LCD_Write_Data(0x72);

    /* VCOM Setting */
    LCD_Write_Cmd(0xBB);
    LCD_Write_Data(0x3D);   //Vcom=1.625V

    /* LCM Control */
    LCD_Write_Cmd(0xC0);
    LCD_Write_Data(0x2C);

    /* VDV and VRH Command Enable */
    LCD_Write_Cmd(0xC2);
    LCD_Write_Data(0x01);

    /* VRH Set */
    LCD_Write_Cmd(0xC3);
    LCD_Write_Data(0x19);

    /* VDV Set */
    LCD_Write_Cmd(0xC4);
    LCD_Write_Data(0x20);

    /* Frame Rate Control in Normal Mode */
    LCD_Write_Cmd(0xC6);
    LCD_Write_Data(0x0F);	//60MHZ

    /* Power Control 1 */
    LCD_Write_Cmd(0xD0);
    LCD_Write_Data(0xA4);
    LCD_Write_Data(0xA1);

    /* Positive Voltage Gamma Control */
    LCD_Write_Cmd(0xE0);
    LCD_Write_Data(0xD0);
    LCD_Write_Data(0x04);
    LCD_Write_Data(0x0D);
    LCD_Write_Data(0x11);
    LCD_Write_Data(0x13);
    LCD_Write_Data(0x2B);
    LCD_Write_Data(0x3F);
    LCD_Write_Data(0x54);
    LCD_Write_Data(0x4C);
    LCD_Write_Data(0x18);
    LCD_Write_Data(0x0D);
    LCD_Write_Data(0x0B);
    LCD_Write_Data(0x1F);
    LCD_Write_Data(0x23);

    /* Negative Voltage Gamma Control */
    LCD_Write_Cmd(0xE1);
    LCD_Write_Data(0xD0);
    LCD_Write_Data(0x04);
    LCD_Write_Data(0x0C);
    LCD_Write_Data(0x11);
    LCD_Write_Data(0x13);
    LCD_Write_Data(0x2C);
    LCD_Write_Data(0x3F);
    LCD_Write_Data(0x44);
    LCD_Write_Data(0x51);
    LCD_Write_Data(0x2F);
    LCD_Write_Data(0x1F);
    LCD_Write_Data(0x1F);
    LCD_Write_Data(0x20);
    LCD_Write_Data(0x23);

    /* Display Inversion On */
    LCD_Write_Cmd(0x21);

    LCD_Write_Cmd(0x29);

    LCD_Address_Set(0, 0, LCD_W - 1, LCD_H - 1);

//    LCD_Clear(0xff00);

    /*打开显示*/
    LCD_PWR(1);
}

void LCD_Clear(uint16_t color)
{
    int i;
    uint8_t data[2] = {0};

    data[0] = color >> 8;
    data[1] = color;

    LCD_Address_Set(0, 0, LCD_W - 1, LCD_H - 1);

    for(i = 0; i < LCD_BUF_SIZE / 2; i++)
    {
        lcd_buf[i * 2] =  data[0];
        lcd_buf[i * 2 + 1] =  data[1];
    }

    LCD_DC(1);

    for(i = 0; i < (LCD_TOTAL_BUF_SIZE / LCD_BUF_SIZE); i++)
    {
        LCD_SPI_Send(lcd_buf, LCD_BUF_SIZE);
    }
}

void xSYS_LCD_Fill(uint8_t* pColor,uint32_t size)
{
    LCD_DC(1);
    SPI3_WriteByte(pColor,size);
}

/****************************************************END OF FILE*****************************************************/
